// pages/mine/mine.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        pushingList: [],
        daiFuKuan: 0,
        daiPingJia: 0,
        daiShouHuo: 0,
        daiFahuo: 0

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.pushingGoodsData();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getUserInfo();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    orderTap: function(e) {
        var _type = e.currentTarget.dataset.type;
        wx.navigateTo({
            url: '../order/order?tab=' + _type + '&hasUserInfo=' + this.data.hasUserInfo,
        })
    },

    bindGetUserInfo(e) {
        if (this.data.userInfo.isLogin == 0) {
            wx.navigateTo({
                url: '../login/loginPhone?fromType=0',
            })
            return;
        }
        let code;
        var that = this;
        wx.login({
            success(res) {
                if (res.code) {
                    // 发起网络请求
                    code = res.code;
                    // 查看是否授权
                    wx.getSetting({
                        withCredentials: true,
                        success(res) {
                            if (res.authSetting['scope.userInfo']) {
                                // 已经授权，可以直接调用 getUserInfo 获取头像昵称
                                wx.getUserInfo({
                                    success(res) {
                                        var tUserInfo = res.userInfo;
                                        // 把数据传给服务器，得到openid
                                        var params = {
                                            code: code,
                                            encryptedData: res.encryptedData,
                                            iv: res.iv,
                                            type: 0
                                        }
                                        app.network.requestPost(app.route.ROUTE_LOGIN_WXUSER, params, '', function (res) {
                                            //res就是我们请求接口返回的数据
                                            if (res.success) {
                                                app.globalData.userInfo = res.context;
                                                wx.navigateTo({
                                                    url: '../login/loginPhone?fromType=0',
                                                })
                                            } else {
                                                wx.showToast({
                                                    title: res.msg,
                                                })
                                            }
                                        }, function (res) {
                                            wx.showToast({
                                                title: '加载数据失败',
                                            })
                                            console.log(res);
                                        })

                                    }
                                })
                            }
                        }
                    })

                } else {
                    console.log('登录失败！' + res.errMsg)
                }
            }
        })
    },
    getUserInfo: function () {
        var that = this;
        var params = {
            userID: app.globalData.userID,
        }
        app.network.requestPost(app.route.ROUTE_MY_CENTER, params, '', function (res) {
            //res就是我们请求接口返回的数据
            if (res.success) {
                app.globalData.userInfo = res.context.userInfo;
                if (res.context.userInfo.name == '' || res.context.userInfo.name ==null) {
                    that.setData({
                        hasUserInfo: false
                    })
                } else {
                    if (res.context.userInfo.isLogin == 0) {
                        that.setData({
                            userInfo: res.context.userInfo,
                            // daiFuKuan: res.context.daiFuKuan,
                            // daiPingJia: res.context.daiPingJia,
                            // daiShouHuo: res.context.daiShouHuo,
                            hasUserInfo: false
                        })
                    } else {
                        that.setData({
                            userInfo: res.context.userInfo,
                            daiFuKuan: res.context.daiFuKuan,
                            daiPingJia: res.context.daiPingJia,
                            daiShouHuo: res.context.daiShouHuo,
                            daiFahuo: res.context.daiFahuo,
                            hasUserInfo: true
                        })
                    }
                }
            } else {
                wx.showToast({
                    title: res.msg,
                })
            }
        }, function (res) {
            wx.showToast({
                title: '加载数据失败',
            })
        })
    },
    pushingGoodsData: function () {
        var that = this;
        var params = {
            userID: app.globalData.userID,
            type: 0
        }
        app.network.requestPost(app.route.ROUTE_PUSHING_GOODS, params, '正在获取数据', function (res) {
            //res就是我们请求接口返回的数据
            if (res.success) {
                var _pushingList = res.context.mySpecialList;
                that.setData({
                    pushingList: _pushingList
                });
            } else {
                wx.showToast({
                    title: res.msg,
                })
            }
        }, function (res) {
            wx.showToast({
                title: '加载数据失败',
            })
        })
    },
    toGoodsTypePage: function(e) {
        var _goodsId = e.currentTarget.dataset.goodsid;
        wx.navigateTo({
            url: '../goodsDetail/goodsDetail?goodsId=' + _goodsId,
        })
    },

    outLoginTap: function(e) {
        var that = this;
        var params = {
            userID: app.globalData.userID
        }
        app.network.requestPost(app.route.ROUTE_MERCHANT_OUT_LOGIN, params, '正在退出', function (res) {
            //res就是我们请求接口返回的数据
            if (res.success) {
                wx.reLaunch({
                    url: '../login/loginPhone',
                })
            } else {
                wx.showToast({
                    title: res.msg,
                })
            }
        }, function (res) {
            wx.showToast({
                title: '加载数据失败',
            })
        })
    }
})